<?php
namespace app\admin\controller;

use app\admin\controller\AdminBaseController;
use think\Cache;
use think\Db;

/**
 * 售票管理
 * Class System
 */
class TicketController extends BaseController {
	
 
    public function logs() {
        $where['type']=1;
		$where['status']=2;
        $list = db('ticketlogs')->where($where)->paginate(15);
        return $this->fetch('logs', ['list' => $list]);
    }
	 public function mfp_logs() {
        $where['type']=2;
		$where['status']=2;
        $list = db('ticketlogs')->where($where)->paginate(15);
        return $this->fetch('mfp_logs', ['list' => $list]);
    }
	//套票管理	
	public function manage(){
		$list=db("ticket_items")->select();
		$this->assign('list',$list);
		return $this->fetch();
	}
	//添加套票项目
	public function add_item(){
		if($_POST){
			$res=add_data("ticket_items",get_query()['query']);
			if($res){
				 $this->success('提交成功',url("manage"));
			}else{
				 $this->success('提交失败');
			}
		}
		return $this->fetch();
	}
	//编辑套票项目
	public function edit_item(){
		if($_POST){
			$res=update_data("ticket_items",get_query()['query']);
			if($res){
				 $this->success('更新成功',url("manage"));
			}else{
				 $this->success('更新失败');
			}
		}
		$item=db("ticket_items")->where("id",input("id"))->find();
		$this->assign('item',$item);
		return $this->fetch();
	}
	public function delete_item(){
		$res=del_data("ticket_items",input("id"));
		if($res){
			 $this->success('删除成功',url("manage"));
		}else{
			 $this->success('删除失败');
		}
	}
    /**
     * 售票配置
     */
    public function index() {
		if ($this->request->isPost()) {
            $ticket_config   = $this->request->post('ticket_config/a');
            $data['value'] = serialize($ticket_config);
            if (Db::name('system')->where('name', 'ticket_config')->update($data) !== false) {
                Cache::set('ticket_config', $ticket_config);
                $this->success('提交成功');
            } else {
                $this->error('提交失败');
            }
        }else{
        	if (Cache::has('')) {
	            $ticket_config = Cache::get('ticket_config');
	        } else {
	            $ticket_config = Db::name('system')->field('value')->where('name', 'ticket_config')->find();
	            $ticket_config = unserialize($ticket_config['value']);
	        }
        	return $this->fetch('index', ['ticket_config' => $ticket_config]);
        }
    }
}
